/*
 * @Author: yangzonglong
 * @Date: 2021-05-16 15:09:39
 * @version: v1.0.0
 * @Descripttion: 定义脚本
 * @LastEditors: yangzonglong
 * @LastEditTime: 2021-05-20 11:32:41
 * @Auditor: 
 */
import React, { useState, useContext, useEffect } from 'react';
import { Input, Checkbox, Row } from 'antd';
import context from '../context';
import { createBpmnXMLNode, BPMN_NODE_TYPE } from './../../../utils/bpmnUtils';
import { GateConditionVarProps, SequenceFlowSelfProps } from './../../../interface';

const NUM_TYPE_LIST: string[] = ['int', 'float', 'number', 'amount'];

interface IProps {
  andGate: GateConditionVarProps[];
  orGate: GateConditionVarProps[];
}

export default ({ andGate, orGate }: IProps) => {
  const { modeler, self } = useContext(context);
  const [isCustJS, setIsCustJS] = useState(false);
  const [custJS, setCustJS] = useState('');
  const [displayJSstr, setDisplayJSstr] = useState(''); // 非用户自定义脚本 根据与 或 或自动生成

  const _self = self as SequenceFlowSelfProps;

  useEffect(() => {
    let { body, isCustomScript } = _self.businessObject?.conditionExpression || {};
    setIsCustJS(isCustomScript || false);
    setCustJS(body || '');
  }, [_self]);

  useEffect(() => {
    _self.businessObject!.conditionExpression = Object.assign(
      createBpmnXMLNode(modeler, BPMN_NODE_TYPE.FORMAL_EXPRESSION),
      { language: "javaScript", isCustomScript: isCustJS, body: isCustJS ? custJS : displayJSstr, }
    )
  }, [_self, modeler, custJS, isCustJS, displayJSstr]);

  useEffect(() => {
    let script = '';

    let effectiveAndGate = andGate.filter(item => {
      return  item.comparesValue && item.term
    });

    let effectiveOrGate = orGate.filter(item => {
      return item.comparesValue && item.variableId && item.term
    });

    const createANDGateScript = () => {
      script += '('
      for (let i of effectiveAndGate) {
        script += `workflow.${i.variableContext === 'global' ? 'variables' : 'actions'}['${i.fieldName || i.variableId}'] ${i.term} ${NUM_TYPE_LIST.includes(i.variableType) ? i.comparesValue : '\'' + i.comparesValue + '\''} &&\n`
      }
      script = script.replace(/&&\n$/, ')')
    }

    const createORGateScript = () => {
      script += '('
      for (let i of effectiveOrGate) {
        script += `workflow.${i.variableContext === 'global' ? 'variables' : 'actions'}['${i.fieldName || i.variableId}'] ${i.term} ${NUM_TYPE_LIST.includes(i.variableType) ? i.comparesValue : '\'' + i.comparesValue + '\''} ||\n`
      }
      script = script.replace(/\|\|\n$/, ')')
    }

    if (effectiveAndGate.length && !effectiveOrGate.length) {
      // ANDGate
      createANDGateScript()
    } else if (!effectiveAndGate.length && effectiveOrGate.length) {
      // ORGate
      createORGateScript()
    } else if (effectiveAndGate.length && effectiveOrGate.length) {
      createANDGateScript()
      script += '\n&&\n'
      createORGateScript()
    }

    setDisplayJSstr(script);
  }, [andGate, orGate]);

  return (
    <>
      <Row>
        <span>{window.$app.t('script')}</span>
        <span className='wingBlank'></span>
        <Checkbox onChange={e => setIsCustJS(e.target.checked)} checked={isCustJS}>
          {window.$app.t('scriptSet')}
        </Checkbox>
      </Row>
      <p/>
      <Input.TextArea
        onChange={e => setCustJS(e.target.value)}
        value={isCustJS ? custJS : displayJSstr}
        disabled={!isCustJS}
        rows={6} />
    </>
  )
}